package wowraidmanager.server.service.wishlist.admin;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.Source;
import wowraidmanager.client.service.wishlist.admin.ImportUrlService;
import wowraidmanager.server.service.wishlist.RaidbossDroplistServiceImpl;
import wowraidmanager.shared.model.wishlist.Droplist;
import wowraidmanager.shared.model.wishlist.RaidbossDroplist;

public class ImportUrlServiceImpl implements ImportUrlService{

    RaidbossDroplistServiceImpl raidbossDroplistService = new RaidbossDroplistServiceImpl();

    public RaidbossDroplist getImportUrlwowhead(String importUrl){
        RaidbossDroplist raidbossdroplist = new RaidbossDroplist();
        ArrayList<Droplist> droplistlist = new ArrayList<Droplist>();
        Source source = null;

        try{
            source = new Source(new URL(importUrl));
        }catch(MalformedURLException e){
            // TODO 自動生成された catch ブロック
            e.printStackTrace();
        }catch(IOException e){
            // TODO 自動生成された catch ブロック
            e.printStackTrace();
        }

        Droplist droplist;

        List<Element> text = source.getAllElements("h1");
        String bossName = text.get(1).toString().substring(4, text.get(1).toString().length() - 5);
        if(bossName.contains("<img")){
            String[] bossNames = bossName.split("<img");
            raidbossdroplist.setBossName(bossNames[0]);
        }else{
            raidbossdroplist.setBossName(bossName);
        }

        text = source.getAllElements("script");
        String scripts = null;
        for(Element scripttext:text){
            if(scripttext.toString().contains("new Listview({template: 'item', id: 'drops',")){
                String[] scripttextlist = scripttext.toString().split(";");
                for(String texttext:scripttextlist){
                    if(texttext.contains("new Listview({template: 'item', id: 'drops',")){
                        scripts = texttext;
                        break;
                    }
                }
                break;
            }
        }

        String[] droplists = scripts.split("data:");
        String[] itemlist = droplists[1].split("\\},\\{");

        for(String item:itemlist){
            droplist = new Droplist();
            droplist.setId(getid(item, "\"id\""));
            String itemName = getitem(item, "\"name\"");
            droplist.setItemColor(itemName.substring(1, 2));
            droplist.setItemName(itemName.substring(2, itemName.length() - 1));
            droplist.setisHeroic(getHeroic(item));

            droplistlist.add(droplist);
        }

        raidbossdroplist.setDroplist(droplistlist);

        raidbossdroplist.setBossId(importUrl.substring(importUrl.indexOf("=") + 1, importUrl.length()));
        return raidbossdroplist;
    }

    public RaidbossDroplist getImportUrlwow(String importUrl){
        RaidbossDroplist raidbossdroplist = new RaidbossDroplist();
        ArrayList<Droplist> droplistlist = new ArrayList<Droplist>();
        Source source = null;

        try{
            source = new Source(new URL(importUrl));
        }catch(MalformedURLException e){
            // TODO 自動生成された catch ブロック
            e.printStackTrace();
        }catch(IOException e){
            // TODO 自動生成された catch ブロック
            e.printStackTrace();
        }

        Droplist droplist;

        List<Element> text = source.getAllElements("td data-raw=");
        String[] bossZoneName = importUrl.substring(importUrl.lastIndexOf("zone/") + 5, importUrl.indexOf("/loot")).split("/");

        raidbossdroplist.setRaidzone(bossZoneName[0]);
        raidbossdroplist.setBossName(bossZoneName[1]);

        for(Element item:text){
            List<Element> itemid = item.getAllElements("a href");
            if(!itemid.isEmpty()){
                droplist = new Droplist();
                String itemidString = itemid.get(0).toString();
                itemidString = itemidString.substring(itemidString.indexOf("item/") + 5, itemidString.indexOf(" class=") - 1);
                droplist.setId(itemidString);

                droplist.setItemColor("3");
                droplist.setItemName(item.getFirstElement("strong").getContent().toString());
                List<Element> isHeroic = itemid.get(0).getAllElements("span");

                if(isHeroic.size() > 1){
                    if(isHeroic.get(1).getAttributeValue("class").toString().matches("icon-heroic-skull")){
                        droplist.setisHeroic(true);
                    }else{
                        droplist.setisHeroic(false);
                    }
                }else{
                    droplist.setisHeroic(false);
                }

                droplistlist.add(droplist);
            }
        }

        raidbossdroplist.setDroplist(droplistlist);

        raidbossdroplist.setBossId("");
        return raidbossdroplist;
    }

    private String getid(String item, String keyword){
        int first = item.indexOf(keyword);
        int last = item.indexOf(",", first);
        return item.substring(first + keyword.length() + 1, last);
    }

    private boolean getHeroic(String item){
        if(item.contains("\"mode\":64,") || item.contains("\"mode\":96,")){
            return true;
        }else{
            return false;
        }
    }

    private String getitem(String item, String keyword){
        int first = item.indexOf(keyword);
        int last = item.indexOf("\",", first);
        return item.substring(first + keyword.length() + 1, last + 1);
    }

    public void setRaidbossDroplist(RaidbossDroplist raidbossDroplist, String raidbossName, String raidZone, String gameVersion){
        raidbossDroplist.setGameVersion(gameVersion);
        raidbossDroplist.setRaidzone(raidZone);

        for(Droplist droplist:raidbossDroplist.getDroplist()){
            droplist.setBossName(raidbossName);
            droplist.setRaidzone(raidZone);
        }

        raidbossDroplistService.setRaidbossDroplist(raidbossDroplist);
    }
}
